package com.tiyende.credit.loop.ui.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.event.TiyendeEventPointHelper
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.ui.popupwindow.TiyendeCommonPopupwindow
import com.tiyende.credit.loop.utils.TiyendeDensityUtil
import com.tiyende.credit.loop.utils.decoration.LinearSpacingItemDecoration
import org.json.JSONObject


/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：流程 - 联系人适配器
 */
class TiyendeAuthenticationStepContactAdapter(private val parentView:ViewGroup, recyclerView: RecyclerView?) :
    CommonRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.tiyende_authentication_step_contact_item) {

    /**
     * 选择联系电话监听
     */
    private var chooseMobileListsner: IChildItemSelectMobileListener? = null
    fun setOnChildItemSelectMobileListener(listener: IChildItemSelectMobileListener){
        chooseMobileListsner = listener
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        val txtAuthenticationContactIndex = helper.getTextView(R.id.txtAuthenticationContactIndex)
        var showContactIndex = String.format(mContext.getString(R.string.tiyende_authentication_step_contact_index),"${position+1}")
        if (position > 0){
            showContactIndex = "$showContactIndex ${mContext.getString(R.string.tiyende_authentication_step_contact_different)}"
        }
        txtAuthenticationContactIndex.text = showContactIndex

        val recyclerViewContact = helper.getView<RecyclerView>(R.id.recyclerViewContact)
        recyclerViewContact.setHasFixedSize(true)
        recyclerViewContact.isNestedScrollingEnabled = false
        recyclerViewContact.isFocusableInTouchMode = false //不需要焦点
        recyclerViewContact.layoutManager = LinearLayoutManager(mContext)
        if (recyclerViewContact.itemDecorationCount == 0){
            val topDecoration = TiyendeDensityUtil.dp2px(16f)
            recyclerViewContact.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        }
        val adapter = TiyendeAuthenticationStepCommonAdapter(recyclerViewContact)
        recyclerViewContact.adapter = adapter
        recyclerViewContact.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { _, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            when(childModel.optString("action")){
                "SELECT" -> {//选择
                    val dataList = arrayListOf<JSONObject>()
                    val dataArray = childModel.optJSONArray("values")
                    if (dataArray != null) {
                        for (i in 0..<dataArray.length()) {
                            dataList.add(dataArray.optJSONObject(i))
                        }
                    }
                    showPopupwindow(childView, dataList) { data ->
                        data?.let {
                            //选择的Other需要设置为空
                            it.put("otherValue","")
                            childModel.put("selectItem", it)
                            adapter.notifyItemChanged(childPosition)

                            val subRemark = "${it.optString("value")}$$${it.optString("duration")}"
                            TiyendeEventPointHelper.addEventPointRequestAction(String.format(TiyendeEventPointType.CLICK.TIYENDE_CLICK_STEP_XX_SELECT,"${model.optString("stepTitle")}_${childModel.optString("paramName")}_${position+1}"), subRemark)
                        }
                    }
                }
                "CONTACT_GROUP_PHONE" -> {//联系电话
                    chooseMobileListsner?.onChildSelectMobileClick(position,childModel)
                }
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null) {
            for (i in 0..<dataArray.length()){
                val contactItem = dataArray.optJSONObject(i)
                contactItem.put("stepTitle",model.optString("stepTitle"))
                contactItem.put("stepType","CONTACT")
                contactItem.put("stepItemIndex",position+1)
                dataList.add(contactItem)
            }
        }
        adapter.data = dataList
    }

    /**
     * choose Mobile
     */
    interface IChildItemSelectMobileListener{
        fun onChildSelectMobileClick(parentPosition: Int,childModel: JSONObject)
    }

    /**
     * SELECT
     */
    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject?)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View?, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        view?.let {
            val selectPopup = TiyendeCommonPopupwindow(mContext)
            selectPopup.setWindowWidth(it.width + TiyendeDensityUtil.dp2px(10f))
            selectPopup.setSelectItemListener(object : TiyendeCommonPopupwindow.IItemClickListener {
                //完成
                override fun onConfirmAction(data: JSONObject?) {
                    listener.onChildSelectClick(data)
                }
                //关闭监听
                override fun onDismissListener() {
                }
            })
            selectPopup.show(it, parentView)
            selectPopup.setBrandList(dataList)
        }
    }
}